/**
 * Created by wushanxin on 14-4-8.
 */

var log = require("../lib/log");
var config = require('../config');
var crypto = require('crypto');
var RUtil = require('./dataformatUtil');


var db = require("../lib/db");
var viruser=require('../models/viruser.js');
var virwifiinfo=require('../models/virwifiinfo.js');
exports.index = function(request, response) {

    var method = request.method || '';

    if(typeof(request.session.admin)=="undefined" || request.session.admin==null || request.session.admin.length==0)
    {
        response.send("<script>window.parent.location.href='login.html';</script>");
        return;
    }



    var virwifiinfodata=RUtil.virwifiinfodata(request);
    virwifiinfodata.retdata.nickname =request.session.admin.nickname;
    virwifiinfodata.retdata.roleinfo = request.session.roleinfo;
    virwifiinfodata.retdata.userinfo = request.session.admin;
    virwifiinfodata.retdata.title="权限管理";
    virwifiinfodata.retdata.authlistemun=RUtil.authlistemun;
    virwifiinfodata.retdata.statlistemun=RUtil.statlistemun;

    var addtitle="添加WiFi热点";
    var edittitle="编辑WiFi热点";
    var norecord="没有任何WiFi热点记录!";
    var errdelrecord="WiFi热点名已经存在！";
    var url="wifiinfo";

    if(method.toUpperCase() === "GET") {
        if(request.query.oper == "add")
        {
            virwifiinfodata.retdata.info = addtitle;
            virwifiinfodata.retdata.success=true;
            response.render("wifiinfo.html",virwifiinfodata);
        }
        else if(request.query.oper == "edit")
        {
            virwifiinfo.selectByWiFiID(request.query.id, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    virwifiinfodata.retdata.info = "载入失败：系统内部错误(错误码："+error.errno+")";
                    virwifiinfodata.retdata.success=false;
                    response.render(url, virwifiinfodata);
                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    virwifiinfodata.retdata.info = norecord;
                    virwifiinfodata.retdata.success=false;
                    response.render(url, virwifiinfodata);
                } else {
                    //存在数据将数据发到页面

                    virwifiinfodata.retdata.info = edittitle;
                    virwifiinfodata.retdata.success=true;
                    virwifiinfodata.virwifiinfo= results[0];

                    response.render(url, virwifiinfodata);
                }
            });
        }


    }

    else if(method.toUpperCase() === "POST") {
        if(request.body.oper == "add")
        {

            var virwifiinfodata = RUtil.virwifiinfo(request);
            virwifiinfodata.userid=request.session.admin.id;
            virwifiinfodata.wifissid=request.body.wifissid;
            virwifiinfodata.wifiauthhour = request.body.wifiauthhour;
            virwifiinfodata.wifiauthbrand = request.body.wifiauthbrand;
            virwifiinfodata.wifipassdomain= request.body.wifipassdomain;
            virwifiinfodata.wifiredirecturl= request.body.wifiredirecturl;
            virwifiinfodata.wifiauthtype= RUtil.getrolenum(request.body.wifiauthtype);
            virwifiinfodata.weixincode= request.body.weixincode;

            virwifiinfo.insert(virwifiinfodata, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    var info = { status:"error",info:'添加失败：系统内部错误(错误码：'+error.errno+')' };
                    if(error.errno==1062)
                        info = { status:"error",info:'添加失败：'+errdelrecord+')' };
                    response.send((info));
                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    var info = { status:"error",info:'添加失败！' };
                    response.send( (info));
                } else {
                    //存在数据将数据发到页面
                    var info = { status:"success",info:'添加成功！' };
                    response.send( (info));
                }
            });
        }
        else if(request.body.oper == "edit")
        {

            var virwifiinfodata = RUtil.virwifiinfodata(request);
            virwifiinfodata.id=request.body.id;
            virwifiinfodata.wifissid=request.body.wifissid;
            virwifiinfodata.wifiauthhour = request.body.wifiauthhour;
            virwifiinfodata.wifiauthbrand = request.body.wifiauthbrand;
            virwifiinfodata.wifipassdomain= request.body.wifipassdomain;
            virwifiinfodata.wifiredirecturl= request.body.wifiredirecturl;
            virwifiinfodata.wifiauthtype= RUtil.getrolenum(request.body.wifiauthtype);
            virwifiinfodata.weixincode= request.body.weixincode;




            virwifiinfo.update(virwifiinfodata, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    var info = { status:"error",info:'更新失败：系统内部错误(错误码：'+error.errno+')' };
                    if(error.errno==1062)
                        info = { status:"error",info:'更新失败：'+errdelrecord+')' };
                    response.send((info));
                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    var info = { status:"error",info:'更新失败！' };
                    response.send( (info));
                } else {
                    //存在数据将数据发到页面
                    var info = { status:"success",info:'更新成功！' };
                    response.send( (info));
                }
            });
        }
    }

};


